/**
 * 
 */
package org.corpus.english.view;

import java.util.Iterator;
import java.util.List;

import org.zkoss.zk.ui.Component;
import org.zkoss.zkplus.databind.TypeConverter;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;

/**
 * @author robbiecheng
 * 
 */
public class WordAttributesConverter implements TypeConverter {

	public Object coerceToBean(Object val, Component comp) {
		return val == null ? null : ((Listitem) val).getLabel();
	}

	public Object coerceToUi(Object val, Component comp) {
		Listitem target = null;
		if (val != null) {
			Listbox lbox = (Listbox) comp;
			String value = (String) val;
			List comps = lbox.getChildren();
			for (Iterator iterator = comps.iterator(); iterator.hasNext();) {
				Listitem item = (Listitem) iterator.next();
				if (value.equals(item.getLabel())) {
					target = item;
					break;
				}
			}
		}
		return target;
	}

}
